package com.ezbuy.common.helper;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

import java.io.*;
import java.util.Map;

public class TemplateGenerater {


    /**
     * FreeMaker的Bean的属性必须有get方法,否则获取不到
     *
     * @param map         数据模型,必须有get方法
     * @param packageName
     * @param className
     * @param fileType
     * @param ftlName
     * @param ftlDir
     * @param javaDir
     */
    public static void generateDir(Object map, String packageName, String className, String fileType, String ftlName, String ftlDir, String javaDir) {
        // step1 创建freeMarker配置实例
        Configuration configuration = new Configuration();
        Writer out = null;
        String filePath = className;
        try {
            // step2 获取模版路径
            configuration.setDirectoryForTemplateLoading(new File(ftlDir));
            configuration.setDefaultEncoding("UTF-8");
            configuration.setObjectWrapper(new DefaultObjectWrapper());
            // step4 加载模版文件
            Template template = configuration.getTemplate(ftlName + ".ftl");
            // step5 生成数据
            //创建文件夹
            String dirPath = Config.projectPath + File.separator + javaDir + File.separator + packageName;
            if (!FileUtils.isFileExists(dirPath)) {
                System.out.println("文件夹不存在,先创建文件夹:" + dirPath);
                FileUtils.createOrExistsDir(dirPath);
            }
            //创建文件
            filePath = javaDir + File.separator + packageName + File.separator + className + fileType;
            File docFile = new File(filePath);
            if (FileUtils.isFileExists(docFile)) {
                System.out.println("文件已经存在,先删除:" + className);
                FileUtils.delete(docFile);
            }
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
            // step6 输出文件
            template.process(map, out);
            System.out.println("生成成功:" + filePath);
        } catch (Exception e) {
            throw new RuntimeException("生成失败：" + filePath, e);
        } finally {
            try {
                if (null != out) {
                    out.flush();
                }
            } catch (Exception e2) {
                throw new RuntimeException("生成异常：" + filePath, e2);
            }
        }
    }


    public static void generate(Map<String, Object> map, String fileName, String ftlName, String ftlDir, String javaDir) {
        // step1 创建freeMarker配置实例
        Configuration configuration = new Configuration();
        Writer out = null;
        try {
            // step2 获取模版路径
            configuration.setDirectoryForTemplateLoading(new File(ftlDir));
            // step4 加载模版文件
            Template template = configuration.getTemplate(ftlName + ".ftl");
            // step5 生成数据
            File docFile = new File(javaDir + File.separator + fileName);
            if (FileUtils.isFileExists(docFile)) {
                System.out.println("文件已经存在,先删除:" + fileName);
                FileUtils.delete(docFile);
            }
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
            // step6 输出文件
            template.process(map, out);
            System.out.println("生成成功:" + fileName);
        } catch (Exception e) {
            throw new RuntimeException("生成失败：" + fileName, e);
        } finally {
            try {
                if (null != out) {
                    out.flush();
                }
            } catch (Exception e2) {
                throw new RuntimeException("生成异常：" + fileName, e2);
            }
        }
    }
}
